//
//  ZFK_galleryMainViewController.m
//  Drops
//
//  Created by SHG on 16/5/20.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_galleryMainViewController.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "GalleryModel.h"
#import "GalleryTableViewCell.h"
#import "DetailGalleryViewController.h"

@interface ZFK_galleryMainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *array;
@property(nonatomic, strong)MBProgressHUD *hud;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, assign)BOOL isUpLoading;
@end

@implementation ZFK_galleryMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.array = [NSMutableArray array];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.page= 0;
    [self creatDataWith:self.page];
    [self creatView];
    self.isUpLoading = NO;
    [self addFooter];
    [self addHeader];
}
//上拉刷新
- (void)addHeader{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        self.page = 0;
        [self creatDataWith:self.page];
    }];
}
//下拉加载
- (void)addFooter {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUpLoading = YES;
        self.page += 1;
        if (self.page < 3) {
         [self creatDataWith:self.page];
        }
    }];
}
//请求数据
-(void)creatDataWith:(NSInteger)page{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://j-world.poco.cn/ajax.php?r=index/index&p=%ld&fresh=0.6784884419757873",self.page] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.isUpLoading == NO) {
            [self.array removeAllObjects];
        }
        NSArray  *arr = [responseObject objectForKey:@"show_list"];
        for (NSDictionary *dic in arr) {
            GalleryModel *model = [[GalleryModel alloc]initWithDic:dic];
            [self.array addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.hud hide:YES afterDelay:0];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        });
        NSLog(@"%@", self.array);
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
//铺试图
-(void)creatView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[GalleryTableViewCell class] forCellReuseIdentifier:@"cell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GalleryModel *model = self.array[indexPath.row];
    GalleryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.imageViewCover sd_setImageWithURL:[NSURL URLWithString:model.img_url]];
    cell.titleLabel.text = model.title;
    cell.detailLabel.text = [NSString stringWithFormat:@"【%@】",model.tag_title];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailGalleryViewController *vc = [[DetailGalleryViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.hidesBottomBarWhenPushed = YES;
    vc.index = indexPath.row;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
